<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/style/admin.css" media="all">
</head>
<body>

<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <a><cite>组件</cite></a>
        <a><cite>数据表格</cite></a>
        <a><cite>开启分页</cite></a>
    </div>
</div>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header" id="layerDemo">文件名：
                    <div class="layui-inline">
                        <input class="layui-input" name="id" id="input_fileName" autocomplete="off">
                    </div>
                    <button class="layui-btn" id="btn_search" data-method="reload">搜索</button>
                    <button class="layui-btn" data-method="fileAdd">新增</button>

                </div>
                <div class="layui-card-body">
                    <table class="layui-hide" id="test-table-page" lay-filter="test"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- 选择角色的按钮 -->
<div class="layui-row" id="popSearchRoleTest" style="display:none;">
    <div class="layui-col-md11">
        <form class="layui-form" lay-filter="formTestFilter" id="submitForm">
            <div class="layui-form-item">
                <label class="layui-form-label">图包名称</label>
                <div class="layui-input-block">
                    <input type="text" name="fileName" id="fileName" lay-verify="fileName" autocomplete="off" oninput="getNumber()"
                           placeholder="请输入标题"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">图包个数</label>
                <div class="layui-input-block">
                    <input type="text" name="fileNumber" id="fileNumber" lay-verify="fileNumber" autocomplete="off" onblur="getPrice()"
                           placeholder="请输入标题"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">图包价格</label>
                <div class="layui-input-block">
                    <input type="text" name="price" id="price" lay-verify="price" autocomplete="off" placeholder="请输入标题"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-block">
                    <textarea class="layui-textarea " name="remark" lay-verify="content" id="remark"></textarea>
                </div>
            </div>
        </form>
    </div>
</div>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-bg-blue layui-icon layui-icon-ok"  lay-event="finish"></a>
    <a class="layui-btn layui-bg-orange   layui-icon layui-icon-release"  lay-event="send"></a>
</script>
<script src="/layui/layui.js"></script>
<script>
    var $ ;
    var table ;
    layui.config({
        base: '/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'form', 'layer'], function () {
        var admin = layui.admin
            , table = layui.table,
            form = layui.form;
        $= layui.$;

        table.render({
            id: 'fileTable',
            elem: '#test-table-page'
            , url: '/receivecontroller/getPage'
            , cols: [
                [
                    {
                        field: 'id', title: 'ID', width: 100, sort: true, templet: function (val) {
                            return val.LAY_INDEX;
                        }
                    }
                    , {field: 'fileName', width: 400, title: '文件名称'}
                    , {field: 'fileNumber', title: '文件数量'}
                    , {field: 'createTime', title: '创建时间',templet:function (val) {
                        var d = new Date(val.createTime);

                        return d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate() + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();
                    }}
                    , {
                    field: 'price', title: '文件价格', templet: function (val) {
                        return val.price + "元";
                    }
                }
                    , {field: 'remark', title: '备注',},
                    {
                        field: 'isFinish', title: '是否完成', templet: function (val) {
                            if (val.isFinish == 0) {
                                return "未完成"
                            }
                            return "已完成";
                        }
                    },
                    {
                        field: 'isSend', title: '是否发送', templet: function (val) {
                            if (val.isSend == 0) {
                                return "未发送"
                            }
                            return "已发送";
                        }
                    }, {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 150}
                ]
            ]
            , page: true
        });
        table.on('tool(test)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            console.log(data);
            if (layEvent === 'finish') {
                layer.confirm('确定发送了吗？', function (index) {
                    finish(data.id);
                });



            } else if (layEvent === 'send') {
                layer.confirm('确定发送了吗？', function (index) {
                    layer.close(index);
                    send(data.id);
                });
            }
        });

         active = {
            reload: function () {
                var demoReload = $('#input_fileName');
                //执行重载
                table.reload('fileTable', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        fileName: demoReload.val()
                    }
                });
            },
            fileAdd: function () {
                layer.open({
                    type: 1,
                    title: '文件添加',
                    area: ['500px', '400px'],
                    content: $('#popSearchRoleTest'),
                    btn: ['确定', '取消'],
                    yes: function (index, layerno) {
                        save();
                        layer.close(index);
                    }
                })
            }
        };
        $('#layerDemo .layui-btn').on('click', function () {
            var othis = $(this), method = othis.data('method');
            active[method] ? active[method].call(this, othis) : '';
        });
    });

    function finish(id) {

        $.ajax({
            url: '/receivecontroller/updateStatus',
            data: {'id': id, 'is_finish': '1'},
            type: "post",
            success: function (data) {
                //刷新表格
                if (data.state==200){
                    $(".layui-laypage-btn")[0].click()
                }else{
                    layer.alert(data.message);
                }
            }, error: function (data) {
                layer.alert(data);
            }
        });
    }
    function send(id) {

        $.ajax({
            url: '/receivecontroller/updateStatus',
            data: {'id': id, 'is_send': '1'},
            type: "post",
            success: function (data) {
                if (data.state==200){
                    $(".layui-laypage-btn")[0].click()
                }else{
                    layer.alert(data.message);
                }
            }, error: function (data) {
                alert(data);
            }
        });
    }
    function save() {
        $.ajax({
            url: '/receivecontroller/SaveOrUpdate',
            data: $('#submitForm').serialize(),
            type: "post",
            success: function (data) {
                if (data.state == 0) {
                    //关闭模块
                    clearForm();
                    //刷新表格
                    $(".layui-laypage-btn")[0].click()
                } else {
                    layer.alert(data.message);
                }
            }, error: function (data) {
                layer.alert('操作失败');
                clearForm();
            }
        });
    }

    function getNumber() {
        var file_name = $("#fileName").val();
        var file_num = file_name.substring(file_name.indexOf('(') + 1, file_name.indexOf(')') - 1);
        $('#fileNumber').val(file_num);
        getPrice();
    }

    function getPrice() {
        var number = $('#fileNumber').val();
        if (number != null) {
            $('#price').val(number * 2)
        }
    }
    function clearForm() {
        $('#fileName').val('');
        $('#fileNumber').val('');
        $('#price').val('');
        $('#remark').val('');
    }
</script>
</body>

</html>